package com.css.common.core.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.Redisson;
import com.css.common.core.utils.RedisLockUtil;

/**
 * @ClassName RestTemplateConfig
 * @Auther qiumengmeng
 * @Date 2024/7/11 上午12:14
 */

@RequiredArgsConstructor
@Configuration
public class RestTemplateConfig {

    private final RedisProperties redisProperties;

    @Bean(name = "restTemplate")
    RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory
                = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(60000);
        httpRequestFactory.setConnectTimeout(60000);
        httpRequestFactory.setReadTimeout(60000);
        return new RestTemplate(httpRequestFactory);
    }

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config
                .useSingleServer()
                .setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
                .setDatabase(redisProperties.getDatabase())
                .setPassword(redisProperties.getPassword())
                .setConnectionPoolSize(8)
                .setConnectionMinimumIdleSize(4)
                .setConnectTimeout(5000);
        return Redisson.create(config);
    }

    @Bean
    public RedisLockUtil redisLockUtil(RedissonClient redissonClient) {
        return new RedisLockUtil(redissonClient);
    }
}


